package com.dwjm.merchant;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.math.BigDecimal;

@WebServlet("/addGoods")
public class AddGoodsServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置编码
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        // 获取前端传来的参数
        String title = req.getParameter("title");
        String priceStr = req.getParameter("price");
        String descr = req.getParameter("descr");
        String img = req.getParameter("img");

        BigDecimal price = new BigDecimal(priceStr);
        int mid = 1; // 测试用，真实应该从 session 中获取商家 ID

        // 构造商品对象
        Goods goods = new Goods();
        goods.setTitle(title);
        goods.setPrice(price);
        goods.setDescr(descr);
        goods.setImg(img);
        goods.setMid(mid);

        // 调用 DAO 添加商品
        MerchantDao merchantDao = new MerchantDaoImpl();
        int result = merchantDao.addGoods(goods);

        // 返回结果
        if (result == 1) {
            resp.getWriter().write("Y");
        } else {
            resp.getWriter().write("N");
        }
    }
}
